/* Project gaigarcounter
PIC16F628A
*/
#include <pic.h>
__CONFIG(
UNPROTECT
& LVPDIS
& BORDIS
& MCLRDIS
& PWRTEN
& WDTDIS
& INTIO
);
__IDLOC(F628);
#define s RA3
#define E RB1
#define RS RA7
#define RW RA6
unsigned char a0[5], a1[5], a2[5], a3[5], a4[5];
unsigned char A[5];
unsigned char aa1, aa2, aa3, aa4, aa5;
unsigned int AA, ui;
unsigned char b[8];
unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
unsigned char i, j, SW, N;
void lcd_w_com4(unsigned char d){
RS = 0;
RW = 0;
PORTB = d << 4;
E = 1;
E = 0;
}
void lcd_w_chr4(unsigned char d){
RS = 1;
RW = 0;
PORTB = d << 4;
E = 1;
E = 0;
}
void lcd_bfck(void){
unsigned char d;
PORTB = 0;
TRISB7 = 1;
RS = 0;
RW = 1;
do{
E = 1;
d = RB7;
E = 0;
E = 1;
E = 0;
} while(d);
PORTB = 0;
TRISB7 = 0;
}
void lcd_putcom(unsigned char d){
lcd_bfck();
lcd_w_com4(d >> 4);
lcd_w_com4(d);
}
void lcd_locate(
unsigned char x,unsigned char y){
lcd_putcom((x + 0x40 * y) | 0x80);
}
void lcd_putchr(unsigned char d){
lcd_bfck();
lcd_w_chr4(d >> 4);
lcd_w_chr4(d);
}
void lcd_puts(const unsigned char *s){
while(*s) lcd_putchr(*s++);
}
void lcd_lclr(unsigned char d){
unsigned char i;
lcd_locate(0, d);
for(i = 0; i < 40; i++)
lcd_putchr(' ');
lcd_locate(0, d);
}
void lcd_putui(
unsigned int ui, unsigned char d){
unsigned char i;
unsigned char buf[5];
for(i = 0; i < 5; i++)
buf[i] = ' ';
i = 4;
do {
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);
for(i = (5 - d); i < 5; i++)
lcd_putchr(buf[i]);
}
void interrupt entry(void){
if(TMR1IF){
TMR1IF = 0;
N ++;
if(N > 9){
N = 0;
a0[j] = aa1;
a1[j] = aa2;
a2[j] = aa3;
a3[j] = aa4;
a4[j] = aa5;
j ++;
if(j > 4) j = 0;
A[0] = a0[0] + a0[1] + a0[2] + a0[3] + a0[4];
A[1] = a1[0] + a1[1] + a1[2] + a1[3] + a1[4];
A[2] = a2[0] + a2[1] + a2[2] + a2[3] + a2[4];
A[3] = a3[0] + a3[1] + a3[2] + a3[3] + a3[4];
A[4] = a4[0] + a4[1] + a4[2] + a4[3] + a4[4];
AA = A[0] + A[1]*10 + A[2]*100 + A[3]*1000 + A[4]*10000;
if(SW == 0){
ui = AA * 6;
lcd_lclr(0);
lcd_puts("GeigerCounter");
lcd_lclr(1);
lcd_locate(0, 1);
lcd_puts(" ");
lcd_putui(ui, 5);
lcd_puts(" cpm");
} else {
b[0] = b8; b[1] = b7; b[2] = b6; b[3] = b5;
b[4] = b4; b[5] = b3; b[6] = b2; b[7] = b1;
lcd_lclr(0);
lcd_puts(" total ");
lcd_lclr(1);
lcd_locate(0, 1);
lcd_puts(" ");
for(i = 0; i < 8; i++){
lcd_putui(b[i], 1);
}
lcd_puts(" count");
}
aa1 = 0; aa2 = 0; aa3 = 0; aa4 = 0; aa5 = 0;
TMR1H = 11; TMR1L = 185;
}
}
if(INTF){
if(SW == 0){
SW = 1;
} else {
SW = 0;
}
for(i = 0; i < 5; i++){
TMR0 = 0;
while(TMR0 < 255);
}
INTF = 0;
}
}
void main(void){
CMCON = 0b00000111;
T1CON = 0b00000001;
PIE1 = 0b00000001;
INTCON = 0b11010000;
TMR1IF = 0;
INTF = 0;
ei();
PR2 = 60;
T2CON = 0b00000100;
CCPR1L = 59;
CCP1CON = 0b00001100;
OPTION = 0b10000101;
PORTA = 0;
TRISA = 0b00001000;
PORTB = 0;
TRISB = 0b00000001;
TMR0 = 0; while(TMR0 < 255);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 80);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 2);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 2);
lcd_w_com4(0x02);
lcd_putcom(0x28);
lcd_putcom(0x01);
lcd_putcom(0x0c);
TMR0 = 0;
while(TMR0 < 255);
aa1 = 0; aa2 = 0; aa3 = 0; aa4 = 0; aa5 = 0;
b1 = 0; b2 = 0; b3 = 0; b4 = 0;
b5 = 0; b6 = 0; b7 = 0; b8 = 0;
a0[0] = 0; a1[0] = 0; a2[0] = 0; a3[0] = 0; a4[0] = 0;
a0[1] = 0; a1[1] = 0; a2[1] = 0; a3[1] = 0; a4[1] = 0;
a0[2] = 0; a1[2] = 0; a2[2] = 0; a3[2] = 0; a4[2] = 0;
a0[3] = 0; a1[3] = 0; a2[3] = 0; a3[3] = 0; a4[3] = 0;
a0[4] = 0; a1[4] = 0; a2[4] = 0; a3[4] = 0; a4[4] = 0;
A[0] = 0; A[1] = 0; A[2] = 0; A[3] = 0; A[4] = 0;
b[0] = 0; b[1] = 0; b[2] = 0; b[3] = 0;
b[4] = 0; b[5] = 0; b[6] = 0; b[7] = 0;
j = 0;
SW = 0;
N = 0;
while(1){
while(s);
while(s == 0);
aa1++; b1++;
if(aa1 > 9){
aa2++; aa1 = 0;
}
if(b1 > 9){
b2++; b1 = 0;
}
if(aa2 > 9){
aa3++; aa2 = 0;
}
if(b2 > 9){
b3++; b2 = 0;
}
if(aa3 > 9){
aa4++; aa3 = 0;
}
if(b3 > 9){
b4++; b3 = 0;
}
if(aa4 > 9){
aa5++; aa4 = 0;
}
if(b4 > 9){
b5++; b4 = 0;
}
if(b5 > 9){
b6++; b5 = 0;
}
if(b6 > 9){
b7++; b6 = 0;
}
if(b7 > 9){
b8++; b7 = 0;
}
}
}